package com.smooth.common.site.common.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.smooth.common.site.common.excep.SiteException;

public class SiteExceptionResolver extends DefaultHandlerExceptionResolver {

	private static final Logger logger = LoggerFactory.getLogger(SiteExceptionResolver.class);


	public SiteExceptionResolver() {
		setOrder(HIGHEST_PRECEDENCE);
	}

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		// 鍏堣褰曟棩蹇�
		logger.error("handle request error. uri: " + request.getRequestURI() + " msg: " + ex.getMessage(), ex);
		if (ex instanceof SiteException) {
			SiteException be = (SiteException) ex;
			ModelAndView result = new ModelAndView(new FastJsonJsonView());
			result.addObject("status", 500);
			result.addObject("message",be.getMessage());
			if(be.getBizCode()!=null) {
				result.addObject("status", be.getBizCode().value);
			}
			return result;
			
		}  else {
			return super.resolveException(request, response, handler, ex);
		}
		
	}

}
